#!/usr/bin/perl


# enumerate all files *.ic run the compiler on them (output as .ic.out)
# and compare to *.tokens

my $ARGC = $#ARGV + 1 ;

if($ARGC < 4) {
	print "Usage: $0 <inputs folder> <temp outputs folder> <compare with folder> <compiler flag>\n";
	exit(0);
}

my $inFolder = $ARGV[0];
my $outFolder = $ARGV[1];
my $cmpFolder = $ARGV[2]; 
my $flag = $ARGV[3];

system "mkdir -p $outFolder";

for my $file (<$inFolder/*.ic>)
{
	print "======== $file ===========\n";
	
	my $cmps = $file;
	$cmps =~ s/\.ic$/\.test/;
	$cmps =~ s/$inFolder/$cmpFolder/;
	
	print "Cmps: ".$cmps."\n";
	
	my $tempOut = $file;
	$tempOut =~ s/\.ic$/\.out/;
	$tempOut =~ s/$inFolder/$outFolder/;
	
	print "Temp Out: ".$tempOut."\n";
	
	system("../run '$file $flag' > $tempOut 2>&1");

	my $diff = `diff $cmps $tempOut`;
	if ($diff) {
		print "  --  Test FAILED :(  --  \n";
		$diff =~ s/^(.*)$/     $1/mg;
		print $diff;
	}
	else {
		print "  ++  Test Passed :)  ++  \n";
	}	
	
	print "\n";
}
